﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Data.OracleClient;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using NetBay.Core.DataAccess;
using NetBay.Core.Extensions;
using NetBay.Core.Log;
using Rio.Framework.RioManager;

#endregion

namespace Rio.Doc.DataAccess
{
    public class DA_Alert
    {
        #region Enum
        /// <summary>
        /// List of DB functions to access data
        /// </summary>
        private enum DbFunction
        {
            [DescriptionAttribute("")]
            None,
            [DescriptionAttribute("P_ALERTES_Add")]
            AddAlert,
            [DescriptionAttribute("P_ALERTES_Get")]
            GetAllAlert,
            [DescriptionAttribute("P_ALERTES_GROUPS_Add")]
            AddAlertForAGroup,
            [DescriptionAttribute("P_ALERTES_DOSSIER_Add")]
            AddAlertForAFolder,
            [DescriptionAttribute("P_ALERTES_Update")]
            UpdateAlert,
            [DescriptionAttribute("P_ALERTES_Get_ByFolderId")]
            GetAllAlertOfAFolder,
            [DescriptionAttribute("P_Folder_Get_ByAlert")]
            GetAllFolderOfAnAlert,
            [DescriptionAttribute("P_Group_Get_ByAlert")]
            GetAllGroupsOfAnAlert,

            

        }
        #endregion


        /// <summary>
        /// Add an alert
        /// </summary>
        /// <param name="p_sAlerteName">the name ofthe alert</param>
        /// <param name="p_sAlerteObject">The object to include in the alert</param>
        /// <param name="p_sAlerteBody">the body of the alert</param>
        /// <param name="p_dAlerteDate">the date when the alert must be sent</param>
        /// <param name="p_lUserId">the user identifier tahtat creates the alert</param>
        /// <param name="action">The action.</param>
        /// <returns>
        /// The database identifier of the new alert
        /// </returns>
        public static long AddAlert(string p_sAlerteName, string p_sAlerteObject, string p_sAlerteBody, DateTime p_dAlerteDate, long p_lUserId, int? action)
        {
            LogManager.Instance().LogDebug("(p_sAlerteName<" + p_sAlerteName + "> p_sAlerteObject<" + p_sAlerteObject + "> p_sAlerteBody<" + p_sAlerteBody + "> p_dAlerteDate<" + p_dAlerteDate + "> p_lUserId<" + p_lUserId + ">)...");
            long l_bRet = 0;
            var db = DbHelper.DataBase;

            var dbCommand = db.GetNewCommand(DbFunction.AddAlert.szGetDescription());
            db.AddInParameter(dbCommand, "p_sAlerteName", DbType.String, p_sAlerteName);
            db.AddInParameter(dbCommand, "p_sAlerteObject", DbType.String, p_sAlerteObject);
            db.AddInParameter(dbCommand, "p_sAlerteBody", DbType.String, p_sAlerteBody);
            db.AddInParameter(dbCommand, "p_dAlerteDate", DbType.DateTime, p_dAlerteDate);
            db.AddInParameter(dbCommand, "p_lUserId", DbType.Int32, p_lUserId);

            if (action.HasValue)
            {
                db.AddInParameter(dbCommand, "p_lActionId", DbType.Int32, action);
            }
            else
            {
                db.AddInParameter(dbCommand, "p_lActionId", DbType.Int32, DBNull.Value);
            }

            using (IDataReader userDataReader = db.ExecuteReader(dbCommand))
            {

                while (userDataReader.Read())
                {
                    l_bRet = Convert.ToInt64(userDataReader["ALERT_ID"]);
                }
            }
            return l_bRet;
        }

        /// <summary>
        /// Get all alerts of today
        /// </summary>
        /// <returns> The database alerts that ocurs today</returns>
        public static IDataReader GetAllAlert()
        {
            LogManager.Instance().LogDebug("(No parameters)...");

            var db = DbHelper.DataBase;

            var dbCommand = db.GetNewCommand(DbFunction.GetAllAlert.szGetDescription());
            
            return db.ExecuteReader(dbCommand);
        }

        /// <summary>
        /// Get all folders from an alert
        /// </summary>
        /// <param name="p_lAlertId">Identifier from an alert</param>
        /// <returns>the folders associated with the alert</returns>
        public static IDataReader GetAllFodlerOfAnAlert(long p_lAlertId)
        {
            LogManager.Instance().LogDebug("(p_lAlertId<" + p_lAlertId + "> )...");
            var db = DbHelper.DataBase;

            var dbCommand = db.GetNewCommand(DbFunction.GetAllFolderOfAnAlert.szGetDescription());
            db.AddInParameter(dbCommand, "p_lAlertId", DbType.Int32, p_lAlertId);

            return db.ExecuteReader(dbCommand);
        }

        /// <summary>
        /// Get all alerts of a folder
        /// </summary>
        /// <param name="p_lFolderTypeId">The identifier of the foldertpe</param>
        /// <param name="p_lFolderId">The identifier of the folder</param>
        /// <returns> The database alerts that ocurs today for a folder</returns>
        public static IDataReader GetAllAlertOfAFolder(long p_lFolderTypeId, long p_lFolderId)
        {
            LogManager.Instance().LogDebug("(p_lFolderTypeId<" + p_lFolderTypeId + ">p_lFolderId<" + p_lFolderId + ">)....");
            var db = DbHelper.DataBase;

            var dbCommand = db.GetNewCommand(DbFunction.GetAllAlertOfAFolder.szGetDescription());
            db.AddInParameter(dbCommand, "p_lFolderTypeId", DbType.Int32, p_lFolderTypeId);
            db.AddInParameter(dbCommand, "p_lFolderId", DbType.Int32, p_lFolderId);

            return db.ExecuteReader(dbCommand);
        }

        /// <summary>
        /// Get all groups attached to the alert
        /// </summary>
        /// <param name="p_lAlertId">The identifier of the Alert</param>
        /// <returns> The database groups attached to an alert</returns>
        public static IDataReader GetAllGroupsOfAnAlert(long p_lAlertId)
        {
            LogManager.Instance().LogDebug("(p_lAlertId<" + p_lAlertId + ">)....");
            var db = DbHelper.DataBase;

            var dbCommand = db.GetNewCommand(DbFunction.GetAllGroupsOfAnAlert.szGetDescription());
            db.AddInParameter(dbCommand, "p_lAlertId", DbType.Int32, p_lAlertId);

            return db.ExecuteReader(dbCommand);
        }

        /// <summary>
        /// Add an alert for a user
        /// </summary>
        /// <param name="p_lGroupId">The identifier of the group</param>
        /// <param name="p_lAlertId">The identifier of the alert</param>
        public static void AddAlertForAGroup(long p_lGroupId, long p_lAlertId)
        {
            LogManager.Instance().LogDebug("(p_lGroupId<" + p_lGroupId + ">p_lAlertId<" + p_lAlertId + ">)....");
            var db = DbHelper.DataBase;

            var dbCommand = db.GetNewCommand(DbFunction.AddAlertForAGroup.szGetDescription(), false);
            db.AddInParameter(dbCommand, "p_lGroupId", DbType.Int32, p_lGroupId);
            db.AddInParameter(dbCommand, "p_lAlertId", DbType.Int32, p_lAlertId);

            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// Add an alert for a folder
        /// </summary>
        /// <param name="p_lAlertId">The identifier of the alert</param>
        /// <param name="p_lFolderTypeId">The identifier of the foldertpe</param>
        /// <param name="p_lFolderId">The identifier of the folder</param>
        public static void AddAlertForAFolder(long p_lAlertId, long p_lFolderTypeId, long p_lFolderId)
        {
            LogManager.Instance().LogDebug("(p_lAlertId<" + p_lAlertId + ">p_lFolderTypeId<" + p_lFolderTypeId + ">p_lFolderId<" + p_lFolderId + ">)....");
            var db = DbHelper.DataBase;

            var dbCommand = db.GetNewCommand(DbFunction.AddAlertForAFolder.szGetDescription(), false);
            db.AddInParameter(dbCommand, "p_lAlertId", DbType.Int32, p_lAlertId);
            db.AddInParameter(dbCommand, "p_lFolderTypeId", DbType.Int32, p_lFolderTypeId);
            db.AddInParameter(dbCommand, "p_lFolderId", DbType.Int32, p_lFolderId);

            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// Update an alert
        /// </summary>
        /// <param name="p_lAlertId">The identifier of the alert</param>
        /// <param name="p_lUserId">The identifier of the user</param>
        /// <param name="p_lStatut">The identifier of the new status</param>
        public static void UpdateAlert(long p_lAlertId, long p_lUserId, long p_lStatut)
        {
            LogManager.Instance().LogDebug("(p_lAlertId<" + p_lAlertId + ">p_lUserId<" + p_lUserId + ">p_lStatut<" + p_lStatut + ">)....");
            var db = DbHelper.DataBase;

            var dbCommand = db.GetNewCommand(DbFunction.UpdateAlert.szGetDescription(), false);
            db.AddInParameter(dbCommand, "p_lAlertId", DbType.Int32, p_lAlertId);
            db.AddInParameter(dbCommand, "p_lUserId", DbType.Int32, p_lUserId);
            db.AddInParameter(dbCommand, "p_lStatut", DbType.Int32, p_lStatut);

            db.ExecuteNonQuery(dbCommand);
        }
    }
}
